import time
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
import no

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://v220-exporter.21silkroad.com/") #出口商

i = no.no

# 出口商登录
driver.find_element(By.ID, 'nameLogin').send_keys("kpcbawuf")
driver.find_element(By.ID, 'codeLogin').send_keys("1234567")
driver.find_element(By.XPATH, "//button[contains(.,'登 录')]").click()
time.sleep(1)


# 新增出口订单
driver.find_element(By.XPATH, "//span[contains(.,'我的业务')]").click()
driver.find_element(By.XPATH, "//span[contains(.,'出口订单')]").click()
driver.find_element(By.XPATH, "//span[contains(.,'新增出口订单')]").click()

max_attempts = 3 #最大尝试次数

for attempt in range(0, max_attempts + 1):
    try:
        # 填写订单基本信息
        # 订单基本信息
        driver.find_element(By.XPATH, "//div[@id='buyer']/span/input").send_keys("CONGNO" + i)  # 买方名称

        driver.find_element(By.XPATH, "//div[@id='telBuyer']/input").send_keys("18818000" + i)  # 买方联系方式

        driver.find_element(By.XPATH, "//*[@id='countryPayment']//input").send_keys("CHN")  # 买方详细地址
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='CHN-中国']").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@id='addrBuyer']/span/input").send_keys("瀚天科技城B区")

        driver.find_element(By.ID, "nameBankself").click()  # 卖方收款信息
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@id='nameBankself_list']/div[1]").click()  # 第一个

        driver.find_element(By.ID, "typeExport").click()  # 出口类型
        time.sleep(1)
        # driver.find_element(By.XPATH, "//div[@id='typeExport_list_0']/div").click()  # 采购出口
        driver.find_element(By.XPATH, "//div[@id='typeExport_list_1']/div").click() #外综出口

        driver.find_element(By.XPATH, "//*[@id='noPi']/span/input").send_keys("CONGNOPI" + i)  # 合同号

        driver.find_element(By.ID, "datePi").click()  # 出口合同签约日期
        time.sleep(1)
        driver.find_element(By.XPATH, "//a[contains(.,'今天')]").click()

        # 合同条款
        driver.find_element(By.ID, "dateDelivery").click()  # 交货期
        time.sleep(1)
        driver.find_elements(By.XPATH, "//a[contains(.,'今天')]")[1].click()

        driver.find_element(By.XPATH, "//*[@id='portStart']//input").click()  # 起运港
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='991101-北京天竺综合保税区']").click()

        driver.find_element(By.XPATH, "//*[@id='portDestination']//input").click()  # 目的港
        time.sleep(1)
        driver.find_elements(By.XPATH, "//div[@title='991201-天津出口加工区']")[1].click()

        driver.find_element(By.XPATH, "//*[@id='countryArrive']//input").send_keys("IDN")  # 抵运国（地区）/最终目的国（地区）
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='IDN-印度尼西亚']").click()

        driver.find_element(By.XPATH, "//input[@id='transmode']").click()  # 成交方式
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='FOB']").click()

        driver.find_element(By.XPATH, "//*[@id='currency']//input").send_keys("美元")  # 付款币制
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='美元']").click()

        # 装运条款
        driver.find_element(By.ID, "cntConta20gp").send_keys("1")  # 所需20GP货柜的数量
        # driver.find_element(By.ID, "cntConta40gp").send_keys("1") #所需20GP货柜的数量
        # driver.find_element(By.ID, "cntConta40hq").send_keys("1") #所需20GP货柜的数量
        # driver.find_element(By.ID, "cntConta45hq").send_keys("1") #所需20GP货柜的数量

        # 商品明细与包装条款
        # 选择商品
        driver.find_element(By.XPATH, "//button[contains(.,'添加商品')]").click()
        time.sleep(1)
        driver.find_element(By.XPATH, "//input[@id='idOwner']").click()  # 供货商端
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[contains(.,'YJB01供货商信息')]").click()

        driver.find_element(By.ID, "idGoods").click()  # 商品名称
        time.sleep(1)
        driver.find_element(By.XPATH, "//span[contains(.,'28383')]").click()

        # 填写条款
        driver.find_element(By.ID, "priceDeal").send_keys("45")  # 单价

        driver.find_element(By.ID, "qtyGoods").send_keys("50")  # 数量

        driver.find_element(By.ID, "shortover").send_keys("6")  # 短溢装(%)

        driver.find_element(By.XPATH, "//*[@id='typeBrand']//input").click()  # 品牌类型
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='无品牌']").click()

        driver.find_element(By.XPATH, "//*[@id='typePack']//input").send_keys("99")  # 包装种类
        time.sleep(1)
        driver.find_element(By.XPATH, "//div[@title='99-其他包装']").click()

        driver.find_element(By.ID, "packLoad").send_keys("2")  # 外包装内容量（件）

        driver.find_element(By.XPATH, "//*[@id='sizePackages']").send_keys("30")  # 外包装尺寸(长/宽/高)(CM)
        driver.find_elements(By.XPATH, "//*[@id='sizePackages']")[1].send_keys("40")
        driver.find_elements(By.XPATH, "//*[@id='sizePackages']")[2].send_keys("50")

        driver.find_element(By.XPATH, "//*[@id='markShipping']//input").send_keys("N/M")  # 唛头

        driver.find_element(By.XPATH, "//button[contains(.,'提 交')]").click()

        print('按1提交，按其他结束')
        x = input()
        if x == '1':
            print("继续")
            driver.find_element(By.XPATH, "//button[contains(.,'提交订单')]").click()
            time.sleep(1)
            driver.find_element(By.XPATH, "//button[contains(.,'确 定')]").click()
        else:
            time.sleep(1)

        # 执行成功，退出循环
        break

    except WebDriverException as e:
        # 如果发生异常，尝试刷新页面
        # print(f"Attempt {attempt} failed: {e}")
        if attempt < max_attempts:
            # 如果还有剩余尝试次数，则刷新页面
            driver.refresh()
            time.sleep(2)
        else:
            # 如果已经是最后一次尝试，则抛出异常
            raise

time.sleep(5)
driver.quit()
print(f"结束运行：{os.path.basename(__file__)}")
